<?php 
	include_once 'dataservice.php';
	include_once 'thuvienham.php';

	class hoadon_db 
	{
		public function Them($MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN)
		{
			$sql = "INSERT INTO `tbl_hoadon`(`MaHoaDon`, `MaQuyenHoaDon`, `MaNguoiDung`, `MaHocVien`, `NgayLap`, `MaChuongTrinh`, `ThanhTien`, `PIN`)
					VALUES ('$MaHoaDon', '$MaQuyenHoaDon', '$MaNguoiDung', '$MaHocVien', '$NgayLap', '$MaChuongTrinh', '$ThanhTien', '$PIN')";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function Xoa($MaHoaDon)
		{
			$sql = "DELETE FROM `tbl_hoadon` WHERE `MaHoaDon` = '$MaHoaDon'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function CapNhat($MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN)
		{
			$sql = "UPDATE `tbl_hoadon` SET ";
					"SET `MaQuyenHoaDon` = '$MaQuyenHoaDon', `MaNguoiDung` = '$MaNguoiDung', `MaHocVien` = '$MaHocVien', `NgayLap` = '$NgayLap', `MaChuongTrinh` = '$MaChuongTrinh', `ThanhTien` = '$ThanhTien'
					WHERE `MaHoaDon` = '$MaHoaDon'";
			$tmp = 0;
			if($MaQuyenHoaDon != -1)
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `MaQuyenHoaDon` = '$MaQuyenHoaDon' ";
			}
			if($MaNguoiDung != "")
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `MaNguoiDung` = '$MaNguoiDung' ";
			}
			if($MaHocVien != "")
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `MaHocVien` = '$MaHocVien' ";
			}
			if($NgayLap != "0000-00-00")
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `NgayLap` = '$NgayLap' ";
			}
			if($MaChuongTrinh != "")
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `MaChuongTrinh` = '$MaChuongTrinh' ";
			}
			if($ThanhTien != -1)
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `ThanhTien` = '$ThanhTien' ";
			}
			if($PIN != "")
			{
				$t = $tmp == 1 ? " , " : "";
				$tmp = 1;
				$sql .= $t . " `PIN` = '$PIN' ";
			}
			$sql .= " WHERE `MaHoaDon` = '$MaHoaDon' ";
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function LayDanhSach($MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN)
		{
			$sql = "SELECT * FROM `tbl_hoadon` WHERE 1  ";
			if($MaHoaDon != -1)
				$sql .= " AND `MaHoaDon` = '$MaHoaDon' ";

			if($MaQuyenHoaDon != -1)
				$sql .= " AND `MaQuyenHoaDon` = '$MaQuyenHoaDon' ";

			if($MaNguoiDung != "")
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";

			if($MaHocVien != "")
				$sql .= " AND `MaHocVien` = '$MaHocVien' ";

			if($NgayLap != "0000-00-00")
				$sql .= " AND `NgayLap` = '$NgayLap' ";

			if($MaChuongTrinh != "")
				$sql .= " AND `MaChuongTrinh` = '$MaChuongTrinh' ";

			if($ThanhTien != -1)
				$sql .= " AND `ThanhTien` = '$ThanhTien' ";
			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";
				
			$sql .= " ORDER BY `MaHoaDon` DESC ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function LayDanhSachPaging($MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN, $Start, $Limit)
		{
			$sql = "SELECT * FROM `tbl_hoadon` WHERE 1  ";
			if($MaHoaDon != -1)
				$sql .= " AND `MaHoaDon` = '$MaHoaDon' ";

			if($MaQuyenHoaDon != -1)
				$sql .= " AND `MaQuyenHoaDon` = '$MaQuyenHoaDon' ";

			if($MaNguoiDung != "")
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";

			if($MaHocVien != "")
				$sql .= " AND `MaHocVien` = '$MaHocVien' ";

			if($NgayLap != "0000-00-00")
				$sql .= " AND `NgayLap` = '$NgayLap' ";

			if($MaChuongTrinh != "")
				$sql .= " AND `MaChuongTrinh` = '$MaChuongTrinh' ";

			if($ThanhTien != -1)
				$sql .= " AND `ThanhTien` = '$ThanhTien' ";
				
			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";
			$sql .= " ORDER BY `MaHoaDon` DESC LIMIT $Start, $Limit ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayDanhSachTheoKhoa($MaKhoa, $MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN)
		{
			$tmp = explode("K", $MaKhoa);
			
			$v = $tmp[1];
			$sql = "SELECT * FROM `tbl_hoadon` WHERE "; 
			$tmp = 0;
			if($MaHoaDon != -1)
			{
				$tmp = 1;
				$sql .= " `MaHoaDon` = '$MaHoaDon' ";
			}
			if($MaQuyenHoaDon != -1)
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaQuyenHoaDon` = '$MaQuyenHoaDon' ";
			}
			if($MaNguoiDung != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaNguoiDung` = '$MaNguoiDung' ";
			}
			if($MaHocVien != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaHocVien` = '$MaHocVien' ";
			}
			if($NgayLap != "0000-00-00")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `NgayLap` = '$NgayLap' ";
			}
			if($MaChuongTrinh != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaChuongTrinh` = '$MaChuongTrinh' ";
			}
			if($ThanhTien != -1)
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `ThanhTien` = '$ThanhTien' ";
			}
			if($PIN != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `PIN` = '$PIN' ";
			}
			$t = $tmp == 1 ? " AND " : "";
			$t = 1;
			$sql .= $t . " `MaHocVien` LIKE '$v.%'";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayDanhSachTheoKhoaPaging($MaKhoa, $MaHoaDon, $MaQuyenHoaDon, $MaNguoiDung, $MaHocVien, $NgayLap, $MaChuongTrinh, $ThanhTien, $PIN, $Start, $Limit)
		{
			$tmp = explode("K", $MaKhoa);
			$v = $tmp[1];
			$sql = "SELECT * FROM `tbl_hoadon` WHERE "; 
			$tmp = 0;
			if($MaHoaDon != -1)
			{
				$tmp = 1;
				$sql .= " `MaHoaDon` = '$MaHoaDon' ";
			}
			if($MaQuyenHoaDon != -1)
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaQuyenHoaDon` = '$MaQuyenHoaDon' ";
			}
			if($MaNguoiDung != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaNguoiDung` = '$MaNguoiDung' ";
			}
			if($MaHocVien != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaHocVien` = '$MaHocVien' ";
			}
			if($NgayLap != "0000-00-00")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `NgayLap` = '$NgayLap' ";
			}
			if($MaChuongTrinh != "")
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `MaChuongTrinh` = '$MaChuongTrinh' ";
			}
			if($ThanhTien != -1)
			{
				$t = $tmp == 1 ? " AND " : "";
				$tmp = 1;
				$sql .= $t . " `ThanhTien` = '$ThanhTien' ";
			}
			$t = $tmp == 1 ? " AND " : "";
			$tmp = 1;
			$sql .= $t . " `MaHocVien` LIKE '$v.%' LIMIT $Start, $Limit ";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
	}

